// pages/orderdetails/orderdetails.js
var common = require('../../js/common.js');
var app = getApp()
var crypt = require('../../js/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var  that =this;
    if (options.id){
      common.request(app.globalData.localUrl, 'orderDetail', {
        id: options.id
      },
        function (res) {
          console.log('订单详情', res)
          if (res.data.code == 1) {
            that.setData({
              address: res.data.data
            })
            if (options.wpy){
              that.setData({
                wpy: options.wpy
              })
            }
            // that.getdatalist();
          }
        }, 'rtype= post');
    }

  },

  //线下支付
  goxianxia: function (e) {
    console.log("id =" + e.target.dataset.id)
    var id = e.target.dataset.id;
    var dh = this.data.mobile;
    wx.redirectTo({
      url: '/pages/xianxiawap/xianxiawap?id=' + id + '&dh=' + dh,
    })
  },

  mobile_input: function (e) {
    this.data.mobile = e.detail.value;
  },
// 微信支付
  btn_sub: function (e) {
    var user_id=wx.getStorageSync('userInfo').id ;
    var order_id = e.target.dataset.id;
    var dh = this.data.mobile;
    to_pay(order_id, user_id,dh)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function to_pay(order_id, user_id,dh) {

  wx.showLoading({
    title: '支付中',
    mask: true
  })
  if(dh){
   var  data1={
      order_id: order_id, user_id: user_id, jisong_numbers:dh
    }
  }else{
    var data1 = {
      order_id: order_id, user_id: user_id
    }
  }
  common.request(app.globalData.wapUrl, 'starpay',
     data1 ,
    function (res) {
      console.log(res)
        var timestamp = (Date.parse(new Date()) / 1000).toString();
        var ak47 = 'appId=' + res.data.data.appid
          + '&nonceStr=' + res.data.data.nonceStr
          + '&package=prepay_id=' + res.data.data.prepay_id
          + '&signType=MD5'
          + '&timeStamp=' + timestamp
          + '&key=' + res.data.data.key;

        var paySign = crypt.hex_md5(ak47).toUpperCase();

        wx.requestPayment({
          'timeStamp': res.data.data.timeStamp,
          'nonceStr': res.data.data.nonceStr,
          'package': res.data.data.package,
          'signType': 'MD5',
          'paySign': res.data.data.paySign,
          /*success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
          },*/
          complete: function (res) {
            wx.hideLoading();

            console.log(res)

            if (res.errMsg == "requestPayment:ok") {
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                mask: true
              })
            }
            else {
              wx.showToast({
                title: '支付失败',
                icon: 'loading',
                mask: true
              })
            }

            setTimeout(function () {
              /*wx.navigateBack({
                delta: 1
              })*/
              wx.redirectTo({
                url: "/pages/smorder/smorder"
              })
            }, 1500);

          }
        })
      

    }
  );
}